package me.leiusual.sharing.util.tool

import android.text.Editable
import android.text.Html
import android.text.Spanned
import android.text.style.AbsoluteSizeSpan
import android.text.style.StrikethroughSpan
import org.xml.sax.XMLReader

/**
 * author: leiusual
 * create on: 2018/12/24
 * description: 自定义html标签解析
 */
class HtmlTagHandler : Html.TagHandler {

  private var startIndex: Int = 0
  private var endIndex: Int = 0

  override fun handleTag(opening: Boolean, tag: String, output: Editable, xmlReader: XMLReader) {
    if (opening) {
      startIndex = output.length
    } else {
      endIndex = output.length

      if (tag == "del") {
        output.setSpan(StrikethroughSpan(), startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
      }

      if (tag.startsWith("size_")) {
        val size = tag.split("_")[1].toInt()
        output.setSpan(AbsoluteSizeSpan(size), startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
      }
    }
  }
}